// 云函数入口文件
const cloud = require('wx-server-sdk')
const tcbRouter = require('tcb-router')
const axios = require('axios')
const BASE_URL = 'https://apis.imooc.com'
const ICODE = 'icode=023BFC723EC68B57'



cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
   const app = new tcbRouter({event})

   app.router('playlist',async(ctx,next) => {
      ctx.body = await cloud.database().collection('playlist').skip(event.start).limit(event.count) //获取当前数据库信息
      .orderBy('createTime','desc')
      .get().then((res) => {
         return res
      })  
   })

   app.router('musiclist',async(ctx,next) => {
      const res = await axios.get(`${BASE_URL}/playlist/detail?id=${parseInt(event.playlistId)}&${ICODE}`)
      ctx.body = res.data
   })

   app.router('allMusicId',async(ctx,next) => {
      const res = await axios.get(`${BASE_URL}/song/detail?ids=${event.allMusicIds}}&${ICODE}`)
      ctx.body = res.data  
   })//获取全部歌曲问题，还没解决

   app.router('musicUrl', async(ctx, next) => {
      const res = await axios.get(`${BASE_URL}/song/url?id=${event.musicId}&${ICODE}`)
      ctx.body = res.data
    })


    app.router('lyric', async(ctx, next) => {
      const res = await axios.get(`${BASE_URL}/lyric?id=${event.musicId}&${ICODE}`)
      ctx.body = res.data
    })
  


   return app.serve()


 
}